package com.css.crm.listener;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.css.crm.cache.CrmCache;
import com.css.crm.service.SystemService;
import com.css.crm.service.impl.SystemServiceImpl;
import com.css.crm.utility.ConstantUtil;
import com.css.crm.utility.PlaceUtil;
import com.css.crm.vo.DiscountCodeVo;
import com.css.crm.vo.DepartmentVo;
import com.css.crm.vo.PlaceVo;
import com.css.crm.vo.StaticReferenceVo;
import com.css.crm.vo.MenuMap;
import com.css.crm.vo.TitleVo;
import com.css.crm.vo.SystemMenu;
import com.css.crm.vo.UserManager;
import com.css.db.MyBatisConfig;

public class ApplicationContextListener implements ServletContextListener {
	private static final Logger log = Logger.getLogger(ApplicationContextListener.class);

	@Override
	public void contextDestroyed(ServletContextEvent event) {

	}

	private void createStaticObjectList() {
		SqlSession session = MyBatisConfig.getSession();
		SystemService systemService = new SystemServiceImpl();
		systemService.setSession(session);

		try {
			// MENU
			MenuMap.menuMapInstance = systemService.getCrmMenuMap();
			SystemMenu.systemMenuList = (ArrayList<SystemMenu>) systemService.getApplTopMenu();

			// im_place data cache
			List<PlaceVo> placeList = systemService.loadPlace();
			PlaceUtil.cacheAllPlace(placeList);

			// Title Selection Items
			TitleVo titleVO = new TitleVo();
			titleVO.setRefType(ConstantUtil.SALUTE_REF_TYPE);
			List<TitleVo> statcRefExtList = systemService.loadTitle(titleVO);
			CrmCache.loadTitleItems(statcRefExtList);

			// FR CHANL Selection Items
			StaticReferenceVo staticReference = new StaticReferenceVo();
			staticReference.setRefType(ConstantUtil.FR_CHANL_REF_TYPE);
			List<StaticReferenceVo> staticReferenceList = systemService.loadStaticReference(staticReference);
			CrmCache.loadFrChanlItems(staticReferenceList);

			// department Selection Items
			List<DepartmentVo> departmentlist = systemService.loadDepartment();
			CrmCache.loadDepartmentItems(departmentlist);

			// Arribute Code Selection Items
			StaticReferenceVo arributeCodeVo = new StaticReferenceVo();
			arributeCodeVo.setRefType(ConstantUtil.ATTRIBUTE_REF_TYPE);
			arributeCodeVo.setRefCde(ConstantUtil.ATTRIBUTE_REF_CODE);
			List<StaticReferenceVo> arributeCodeList = systemService.loadStaticReference(arributeCodeVo);
			CrmCache.loadArributeCodeItems(arributeCodeList);

			// Discount Code Selection Items
			DiscountCodeVo discountCodeVo = new DiscountCodeVo();
			List<DiscountCodeVo> discountCodeList = systemService.loadPsDisc(discountCodeVo);
			CrmCache.loadDiscountCodeItems(discountCodeList);

			// Service Code Selection Items
			StaticReferenceVo serviceCodeVo = new StaticReferenceVo();
			serviceCodeVo.setRefType(ConstantUtil.SERVICE_REF_TYPE);
			List<StaticReferenceVo> serviceCodeList = systemService.loadStaticReference(serviceCodeVo);
			CrmCache.loadServiceCodeItems(serviceCodeList);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		log.info("Loading application context");
		event.getServletContext().setAttribute(UserManager.BEAN_NAME, new UserManager());
		log.info("Loading application context completed");
		createStaticObjectList();
	}

}
